package com.zhiwen.lockdemo;

/**
 * @Auther: wuzhiwen
 * @Date: 2018/12/19 16:10
 * @Description:
 */
public class DiedLock {
    public static void main(String[] args) {
        Demo demo1 = new Demo();
        demo1.flag = 0;
        Demo demo2 = new Demo();
        demo2.flag = 1;
        new Thread(demo1).start();
        new Thread(demo2).start();
    }
}

class Demo implements Runnable {
    int flag;
    private static Object o1 = new Object(), o2 = new Object();

    public void run() {
        if ( flag == 0) {
            synchronized (o1){
                try {
                    Thread.sleep(500);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                synchronized (o2){
                    System.out.println(flag);
                }
            }
        }

        if ( flag == 1) {
            synchronized (o2){
                try {
                    Thread.sleep(400);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                synchronized (o1){
                    System.out.println(flag);
                }
            }
        }
    }
}